import {showToast} from '../../../share/common/util/util.js'
import router from '@system.router';
import Log from '../../../share/common/util/log.js'
import bleOperator from '../../../../js/share/common/opinterface/bleoperator.js'
import bleDataUtils from '../../../../js/share/common/util/bleDataUtils.js'
import config from '../../../share/common/config/config.js'

const TAG = 'issued';

export default {
    data: {
        title: '',
        inputName: '',
        tagId: '',
        macAddr: '',
        inputTitle: '',

        issuedName1: '',
        threshold1: '',
        writeTagId1: '',

        issuedName2: '',
        threshold2: '',
        writeTagId2: ''
    },
    onInit() {
        if (this.params == undefined) {
            Log.error('Params is undefined!');
        } else {
            let paramJson = JSON.parse(this.params);
            this.tagId = paramJson.tagId;
            this.macAddr = paramJson.macAddr;
            this.threshold1 = paramJson.threshold1;
            this.threshold2 = paramJson.threshold2;
        }
        switch (this.tagId) {
        //            case '0001':
        //                this.title = '火焰感应'
        //                break;
        //            case '0003':
        //                this.title = '烟感温湿度'
        //                break;
            case '0007':
                this.title = '土壤湿度传感器阀值设置'
                this.inputTitle = '湿度设置'
                this.issuedName1 = '最高湿度'
                this.writeTagId1 = '0008'
                break;
            case '0009':
                this.title = '环境温湿度阀值设置'
                this.inputTitle = '温湿度设置'
                this.issuedName1 = '最高温度'
                this.issuedName2 = '最高湿度'
                this.writeTagId1 = '000b'
                this.writeTagId2 = '000c'
                break;
            case '000D':
                this.title = '酒精乙醇感应板阀值设置'
                this.inputTitle = '酒精浓度设置'
                this.issuedName1 = '最高浓度'
                this.writeTagId1 = '000e'
                break;
        //            case '0013':
        //                this.title = '空气质量'
        //                break;
            case '001C':
                this.title = '照明功能板模式设置'
                this.inputTitle = '照明模式设置：'
                this.writeTagId1 = '001C'
                break;
            default:
                break;
        }
    },
    //下发阀值数据
    writeBleData(data) {
        Log.info("writeBleData value = " + data);
        bleOperator.writeBleCharacteristicValue(this.macAddr, config.Service_UUID, config.CharacterWrite_UUID, data, callbackJson => {
            Log.info("writeBleCharacteristicValue : " + JSON.stringify(callbackJson.data.writeData));
            showToast("阀值设置成功!")
        })
    },
    change1(e) {
        this.threshold1 = e.value
    },
    enterkeyClick1(e) {
        showToast("value: " + this.threshold1)
        if (this.tagId == "000D") {
            let data = bleDataUtils.getTagsCommandHexStr(this.writeTagId1, config.MType[0], parseInt(this.threshold1) * 10)
            this.writeBleData(data)
        } else {
            let data = bleDataUtils.getTagsCommandHexStr(this.writeTagId1, config.MType[0], parseInt(this.threshold1))
            this.writeBleData(data)
        }

    },
    change2(e) {
        this.threshold2 = e.value
    },
    enterkeyClick2(e) {
        showToast("value: " + this.threshold2)
        let data = bleDataUtils.getTagsCommandHexStr(this.writeTagId2, config.MType[0], parseInt(this.threshold2))
        this.writeBleData(data)
    },
    onRadioChange(inputValue, e) {
        if (inputValue === e.value) {
            this.threshold1 = e.value
            let data = bleDataUtils.getTagsCommandHexStr(this.writeTagId1, config.MType[0], parseInt(this.threshold1))
            this.writeBleData(data)
        }
    },

    backClicked() {
        router.back()
    },
    onDestroy() {

    }
}
